{extend name="common@base/common" /}

{block name="style"}
<link href="__CSS__/main.min.css" rel="stylesheet" type="text/css"/>
{/block}

{block name="body"}
<div class="container">
  <ol class="breadcrumb">
      <li><a href="{:url('index/index')}">演示首页</a></li>
      <li>上下文菜单</li>
  </ol>
<section class="demo-section">
<div id="pageContent">




<article><p>上下文菜单插件允许你让用户在鼠标所在的位置（或附近）弹出一个菜单面板，提供预设的列表选项供用户点击选择。</p><div class="alert alert-warning">
  <h4>兼容性问题</h4>
  <p>在触摸屏及移动设备上无法启用需要右键点击触发的上下文菜单。</p>
</div><p>有被动式和主动式两种方式来弹出上下文菜单。</p></article>



<section>
  <header><h3>被动式</h3></header>
  <article><p>被动式使用 <code>$().contextmenu(options)</code> 初始化上下文菜单。此方式自动监听所选操作元素上的 <code>contextmenu</code> 鼠标事件（可以通过 <code>trigger</code> 选项更改监听事件），在事件触发时弹出上下文菜单。</p><div class="example hl-primary" id="contextMenuExample1">
在此区域点击右键显示上下文菜单。
  <div class="text-info"></div>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"hl-primary"</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"contextMenuExample1"</span><span class="tag">&gt;</span><span class="pln">
在此区域点击右键显示上下文菜单。
  </span><span class="tag">&lt;div</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"text-info"</span><span class="tag">&gt;&lt;/div&gt;</span><span class="pln">
</span><span class="tag">&lt;/div&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#contextMenuExample1'</span><span class="pun">).</span><span class="pln">contextmenu</span><span class="pun">({</span><span class="pln">
    items</span><span class="pun">:</span><span class="pln"> </span><span class="pun">[{</span><span class="pln">
        label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'新建项目'</span><span class="pun">,</span><span class="pln">
        onClick</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            alert</span><span class="pun">(</span><span class="str">'你点击了新建项目'</span><span class="pun">);</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
    </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
      type</span><span class="pun">:</span><span class="pln"> </span><span class="str">'divider'</span><span class="pln">
    </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'复制'</span><span class="pun">,</span><span class="pln">
    </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'剪切'</span><span class="pun">,</span><span class="pln">
    </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'粘贴'</span><span class="pun">,</span><span class="pln">
    </span><span class="pun">}],</span><span class="pln">
    onClickItem</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">item</span><span class="pun">,</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $</span><span class="pun">(</span><span class="str">'#contextMenuExample1 .text-info'</span><span class="pun">).</span><span class="pln">text</span><span class="pun">(</span><span class="str">'你刚刚点击了 "'</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> item</span><span class="pun">.</span><span class="pln">label </span><span class="pun">+</span><span class="pln"> </span><span class="str">'"'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">});</span></code></pre></article></section>







<section><header><h3>主动式</h3></header><article><p>主动式使用 <code>$.zui.ContextMenu.show(items, options, callback)</code> 显示上下文菜单。此方式可以手动在合适的时机（如点击某个按钮时）主动弹出上下文菜单。</p><p>主动式 <code>$.zui.ContextMenu.show</code> 方法参数定义如下：</p><ul>
<li><code>items</code>：菜单条目对象数组；</li>
<li><code>options</code>：菜单自定义选项；</li>
<li><code>callback</code>：菜单显示完成的回调函数。</li>
</ul><div class="example">
  <button id="contextMenuExample2" type="button" class="btn btn-primary">点击按钮弹出上下文菜单</button>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"contextMenuExample2"</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="tag">&gt;</span><span class="pln">点击按钮弹出上下文菜单</span><span class="tag">&lt;/button&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#contextMenuExample2'</span><span class="pun">).</span><span class="pln">on</span><span class="pun">(</span><span class="str">'click'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ContextMenu</span><span class="pun">.</span><span class="pln">show</span><span class="pun">([{</span><span class="pln">
        label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'新建项目'</span><span class="pun">,</span><span class="pln">
        onClick</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            alert</span><span class="pun">(</span><span class="str">'你点击了新建项目'</span><span class="pun">);</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
    </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        type</span><span class="pun">:</span><span class="pln"> </span><span class="str">'divider'</span><span class="pln">
    </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'复制'</span><span class="pun">,</span><span class="pln">
    </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'剪切'</span><span class="pun">,</span><span class="pln">
    </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'粘贴'</span><span class="pun">,</span><span class="pln">
    </span><span class="pun">}],</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        event</span><span class="pun">:</span><span class="pln"> e
    </span><span class="pun">},</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'上下文菜单已显示。'</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">});</span><span class="pln">
</span><span class="pun">});</span></code></pre></article></section>




<section><header><h3>选项</h3></header><article><p>大部分选项同时适用与主动式和被动式，一些特殊的选项只适用于单一方式会加以注明。</p><div class="table-responsive"><table class="table table-bordered">
  <thead>
    <tr>
      <th style="width: 140px">选项</th>
      <th style="width: 150px">名称</th>
      <th style="width: 150px">可用值</th>
      <th>说明</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>items</code></td>
      <td>菜单条目数组</td>
      <td>例如: <code>[{label: '复制'}]</code>，或者使用回调函数 <code>function(options)</code></td>
      <td>菜单条目对象可用属性参加下文；当使用回调函数时可以使用函数动态的返回菜单条目来创建菜单，毁掉函数参数 <code>options</code> 为菜单创建的所有选项对象。</td>
    </tr>
    <tr>
      <td><code>x</code>（仅主动式）</td>
      <td>菜单弹出时水平方向位置（距离页面窗口左侧）</td>
      <td>例如 <code>100</code></td>
      <td>如果是从页面鼠标事件对象获取，通常是 <code>event.clientX</code> 属性。</td>
    </tr>
    <tr>
      <td><code>y</code>（仅主动式）</td>
      <td>菜单弹出时垂直方向位置（距离页面窗口顶部）</td>
      <td>例如 <code>50</code></td>
      <td>如果是从页面鼠标事件对象获取，通常是 <code>event.clientY</code> 属性。</td>
    </tr>
    <tr>
      <td><code>event</code>（仅主动式）</td>
      <td>菜单弹出触发时的鼠标事件对象</td>
      <td>例如点击按钮时回调函数的 <code>event</code> 对象</td>
      <td>如果指定了此对象，则 <code>x</code> 和 <code>y</code> 选项无需手动指定，会自动通过事件对象进行计算。</td>
    </tr>
    <tr>
      <td><code>trigger</code>（仅被动式）</td>
      <td>显示菜单的触发事件</td>
      <td>默认为 <code>'contextmenu'</code></td>
      <td>可以为 <code>'mousedown'</code>、<code>'click'</code> 等鼠标事件。</td>
    </tr>
    <tr>
      <td><code>selector</code>（仅被动式）</td>
      <td>触发事件选择器</td>
      <td>默认为 <code>''</code></td>
      <td>可以使用 jQuery 选择器来指定在哪些元素上响应触发事件，例如设置此选项为 <code>'li'</code>，则只在 <code>&lt;li&gt;</code> 元素上触法菜单显示。</td>
    </tr>
    <tr>
      <td><code>animation</code></td>
      <td>菜单显示时的动画选项</td>
      <td>默认为 <code>false</code>，不启用动画效果</td>
      <td>
        <p>可用的动画效果包括：</p>
        <ul>
          <li><code>fade</code>：启用渐隐渐现效果；</li>
          <li><code>scale</code>：启用缩放效果；</li>
          <li><code>fade scale</code>：同时启用渐隐渐现效果和缩放效果。</li>
        </ul>
      </td>
    </tr>
    <tr>
      <td><code>duration</code></td>
      <td>动画效果执行的时间</td>
      <td>默认为 <code>200</code></td>
      <td>单位为毫秒</td>
    </tr>
    <tr>
      <td><code>itemCreator</code></td>
      <td>菜单条目元素生成器</td>
      <td>默认为 <code>null</code>，或 <code>function(item, index, options)</code></td>
      <td>
        <p>如果指定了回调函数，则在创建菜单条目元素时，使用此方法来生成每个菜单条目的 $dom 元素，需要返回 html 代码或者 jQuery 元素对象。回调函数各项参数定义如下：</p>
        <ul>
          <li><code>item</code>，当前操作的菜单项对象；</li>
          <li><code>index</code>，当前操作的菜单项在菜单中的索引（从 0 开始）；</li>
          <li><code>options</code>，要创建的菜单所有选项；</li>
        </ul>
      </td>
    </tr>
    <tr>
      <td><code>onClickItem</code></td>
      <td>当用户点击了菜单项时的回调函数</td>
      <td>默认为 <code>null</code>，或 <code>function(item, $item, e)</code></td>
      <td>
        <p>回调函数个参数定义如下：</p>
        <ul>
          <li><code>item</code>，当前操作的菜单项对象；</li>
          <li><code>$item</code>，当前点击的菜单项元素；</li>
          <li><code>e</code>，鼠标点击事件对象；</li>
        </ul>
      </td>
    </tr>
    <tr>
      <td><code>onShow</code></td>
      <td>准备显示菜单时的回调函数</td>
      <td>默认为 <code>null</code>，或 <code>function()</code></td>
      <td>
      </td>
    </tr>
    <tr>
      <td><code>onShown</code></td>
      <td>已经完成显示菜单时的回调函数</td>
      <td>默认为 <code>null</code>，或 <code>function()</code></td>
      <td>
      </td>
    </tr>
    <tr>
      <td><code>onHide</code></td>
      <td>准备隐藏菜单时的回调函数</td>
      <td>默认为 <code>null</code>，或 <code>function()</code></td>
      <td>
      </td>
    </tr>
    <tr>
      <td><code>onHidden</code></td>
      <td>已经完成隐藏菜单时的回调函数</td>
      <td>默认为 <code>null</code>，或 <code>function()</code></td>
      <td>
      </td>
    </tr>
  </tbody>
</table></div></article></section>








<section><header><h3>菜单条目对象属性</h3></header><article><p>菜单条目数组中的每个对象对应菜单列表上的一个条目。每一个菜单条目对象上的可用属性包括：</p><div class="table-responsive"><table class="table table-bordered">
  <thead>
    <tr>
      <th style="width: 140px">选项</th>
      <th style="width: 150px">名称</th>
      <th style="width: 150px">可用值</th>
      <th>说明</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>type</code></td>
      <td>条目类型</td>
      <td>默认 <code>''</code></td>
      <td>
        <p>默认可用的类型包括：</p>
        <ul>
          <li><code>''</code> 或 <code>'item'</code>：默认可点击的条目；</li>
          <li><code>divider</code> 或 <code>'seperator'</code>：分割线；</li>
        </ul>
      </td>
    </tr>
    <tr>
      <td><code>label</code></td>
      <td>条目显示的内容</td>
      <td><code>''</code></td>
      <td></td>
    </tr>
    <tr>
      <td><code>html</code></td>
      <td>是否使用将条目内容作为 html 显示</td>
      <td><code>false</code>(默认)</td>
      <td>如果使用 <code>true</code> 则会使用 jQuery 的 <code>$().html()</code> 方法来插入内容到界面上，要启用此选项确保要插入的内容是安全的；如果此选项为一个字符串，则支持使用此字符串作为 html 内容显示。</td>
    </tr>
    <tr>
      <td><code>url</code></td>
      <td>条目作为链接时的链接地址</td>
      <td>默认 <code>'###'</code></td>
      <td></td>
    </tr>
    <tr>
      <td><code>disabled</code></td>
      <td>是否禁用点击</td>
      <td>默认 <code>false</code></td>
      <td>如果设置为 <code>true</code>，此条目会变灰显示，并且用户点击不会触发 <code>onClick</code> 事件。</td>
    </tr>
    <tr>
      <td><code>className</code></td>
      <td>条目元素上要添加的类</td>
      <td>默认 <code>''</code></td>
      <td></td>
    </tr>
    <tr>
      <td><code>style</code></td>
      <td>条目元素上要添加的样式</td>
      <td>默认 <code>null</code></td>
      <td>例如 <code>{color: 'red'}</code></td>
    </tr>
    <tr>
      <td><code>onClick</code></td>
      <td>当点击此条目时的回调函数</td>
      <td>默认为 <code>null</code>, function(event)</td>
      <td>
        <p>回调函数参数定义如下：</p>
        <ul>
          <li><code>event</code>：鼠标点击事件对象；</li>
        </ul>
      </td>
    </tr>
  </tbody>
</table></div><p>当一个菜单条目对象只有显示文本是关键信息或该对象表示分割线时还可以简单使用字符串来表示。</p><p>例如：</p><ul>
<li><code>'divider'</code>、<code>'-'</code>、<code>'|'</code> 都相当于 <code>{type: 'divider'}</code>；</li>
<li><code>'复制'</code> 相当于 <code>{label: '复制'}</code>。</li>
</ul><p>菜单条目数组也可以简写为如下形式：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">// 简写形式</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> items1 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">'新建项目'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'-'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'复制'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'粘贴'</span><span class="pun">];</span><span class="pln">

</span><span class="com">// 完整写法</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> items2 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'新建项目'</span><span class="pun">},</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">type</span><span class="pun">:</span><span class="pln"> </span><span class="str">'divider'</span><span class="pun">},</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'复制'</span><span class="pun">},</span><span class="pln">
    </span><span class="pun">{</span><span class="pln">label</span><span class="pun">:</span><span class="pln"> </span><span class="str">'粘贴'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">];</span></code></pre></article></section>

<script>

$(function(){
  afterPageLoad();
});

function afterPageLoad() {
    $('#contextMenuExample1').contextmenu({
        items: [{
            label: '新建项目',
            onClick: function() {
                alert('你点击了新建项目');
            }
        }, {
          type: 'divider'
        }, {
            label: '复制',
        }, {
            label: '剪切',
        }, {
            label: '粘贴',
        }],
        onClickItem: function(item, e) {
            $('#contextMenuExample1 .text-info').text('你刚刚点击了 "' + item.label + '"');
        }
    });

    $('#contextMenuExample2').on('click', function(e) {
        $.zui.ContextMenu.show([{
            label: '新建项目',
            onClick: function() {
                alert('你点击了新建项目');
            }
        }, {
            type: 'divider'
        }, {
            label: '复制',
        }, {
            label: '剪切',
        }, {
            label: '粘贴',
        }], {
            event: e
        }, function() {
            console.log('上下文菜单已显示。');
        });
    });
}
</script><section><header><h3>方法</h3></header><article><h4><code>show(options, callback)</code></h4><p>立即显示菜单。当使用主动式时，该方法还有如下形式：<code>show(items, options, callback)</code>。</p><p>主动式用法：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="kwd">var</span><span class="pln"> items </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">'新建项目'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'-'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'复制'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'粘贴'</span><span class="pun">];</span><span class="pln">
$</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ContextMenu</span><span class="pun">.</span><span class="pln">show</span><span class="pun">(</span><span class="pln">items</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">x</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100</span><span class="pun">});</span></code></pre><p>被动式用法：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">// 获取实例对象</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> contextMenu </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#myContextMenu'</span><span class="pun">).</span><span class="pln">data</span><span class="pun">(</span><span class="str">'zui.contextmenu'</span><span class="pun">);</span><span class="pln">

</span><span class="com">// 调用实例方法</span><span class="pln">
contextMenu</span><span class="pun">.</span><span class="pln">show</span><span class="pun">({</span><span class="pln">x</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100</span><span class="pun">,</span><span class="pln"> y</span><span class="pun">:</span><span class="pln"> </span><span class="lit">100</span><span class="pun">});</span></code></pre><h4><code>hide(callback)</code></h4><p>立即隐藏菜单。</p><p>主动式用法：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ContextMenu</span><span class="pun">.</span><span class="pln">hide</span><span class="pun">();</span></code></pre><p>被动式用法：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">// 获取实例对象</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> contextMenu </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#myContextMenu'</span><span class="pun">).</span><span class="pln">data</span><span class="pun">(</span><span class="str">'zui.contextmenu'</span><span class="pun">);</span><span class="pln">

</span><span class="com">// 调用实例方法</span><span class="pln">
contextMenu</span><span class="pun">.</span><span class="pln">hide</span><span class="pun">();</span></code></pre><h4><code>listenMouseMove()</code>（仅主动式）</h4><p>监听鼠标移动位置。调用此方法之后，会自动监听并记录当前用户鼠标位置，在调用 <code>show()</code> 方法时不再需要使用 <code>x</code>、<code>y</code>、<code>event</code> 等选项来手动指定弹出位置。</p><p>用法：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ContextMenu</span><span class="pun">.</span><span class="pln">listenMouseMove</span><span class="pun">();</span></code></pre><h4><code>stopListenMouse()</code>（仅主动式）</h4><p>停止监听鼠标移动位置。</p><p>用法：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ContextMenu</span><span class="pun">.</span><span class="pln">stopListenMouse</span><span class="pun">();</span></code></pre><h4><code>destory()</code>（仅被动式）</h4><p>销毁元素上绑定的对象实例，执行此方法之后不再监听特定事件自动弹出显示上下文菜单。</p><p>用法：</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">// 获取实例对象</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> contextMenu </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#myContextMenu'</span><span class="pun">).</span><span class="pln">data</span><span class="pun">(</span><span class="str">'zui.contextmenu'</span><span class="pun">);</span><span class="pln">

</span><span class="com">// 调用实例方法</span><span class="pln">
contextMenu</span><span class="pun">.</span><span class="pln">destory</span><span class="pun">();</span></code></pre></article></section></div>
</section>
{/block}